package cn.pegasus.teamwork.pegasus.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import cn.iyizhan.teamwork.base.util.PageObject;
import cn.iyizhan.teamwork.base.util.ParameterEunm;
import cn.iyizhan.teamwork.base.util.ReMessage;
import cn.pegasus.teamwork.manager.controller.BaseManagerController;
import cn.pegasus.teamwork.pegasus.entity.BussNewsInfo;
import cn.pegasus.teamwork.pegasus.mapper.BussNewsInfoMapper;
import cn.pegasus.teamwork.pegasus.service.BussNewsInfoService;
import cn.pegasus.teamwork.util.DateCompareUtil;
import cn.pegasus.teamwork.util.OSSClientUtil;

@Controller
@RequestMapping("/news")
public class NewsController extends BaseManagerController<BussNewsInfo, BussNewsInfoMapper>{
	
	private static final Logger logger = LoggerFactory.getLogger(NewsController.class);
	@Autowired
	private BussNewsInfoService newsInfoService;

	/**
	 * 查询服务列表
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(@RequestParam Map<String, Object> map){
		ModelAndView view = new ModelAndView("news/news-list");
		if(map.isEmpty()){
			map.put("pageData", 10);
			map.put("curPage", 1);
		}
		try{
			PageObject<BussNewsInfo> dicList = newsInfoService.PageList(map);
			view.addObject("list", dicList);
			view.addObject("map", map);
		}catch(Exception e){
			logger.error("查询服务列表信息异常",e.getMessage());
		}
		return view;
	}
	
	/**
	 * 添加新增页面
	 */
	 @RequestMapping(value="/goAdd" ,method = {RequestMethod.GET})
	 public ModelAndView goAdd(@RequestParam(value="type",required = false) String type,
			 @RequestParam(value="id",required = false) Integer id)throws Exception{
		 if(null == type){
			 ModelAndView view = new ModelAndView("news/news-add");
			 return view;
		 }else{
			 ModelAndView view = new ModelAndView("news/news-edit");
			 BussNewsInfo news = newsInfoService.findNewsInfo(id);
			 view.addObject("info", news);
			 return view;
		 } 
	 }
	 
	 /**
	  * 新增
	  * newsType 1-帮助中心；2-关于我们；3-其他类型,
	  */
	 @ResponseBody
	 @RequestMapping(value = "/addNews", method = RequestMethod.POST)
	 public String addNews(HttpServletRequest request,
			 @RequestParam(value = "newsType", required = false) Integer newsType,
			 @RequestParam(value = "newsTitle", required = false) String newsTitle,
			 @RequestParam(value = "newsSummary", required = false) String newsSummary,
			 @RequestParam(value = "newsReleaseDate", required = false) String newsReleaseDate,
			 @RequestParam(value = "newsContent", required = false) String newsContent,
			 @RequestParam(value = "newsKeyWord", required = false) String newsKeyWord){
        try {
        	MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request;
        	MultipartFile file = fileRequest.getFile("file");
        	BussNewsInfo news = new BussNewsInfo();
        	news.setNewsType(newsType);
        	news.setNewsTitle(newsTitle);
        	news.setNewsSummary(newsSummary);
        	news.setNewsContent(newsContent);
        	news.setNewsKeyWord(newsKeyWord);
        	if(StringUtils.isNotBlank(newsReleaseDate)){
        		news.setNewsReleaseDate(DateCompareUtil.getDateToString(newsReleaseDate));
        	}
        	if (file != null && StringUtils.isNotBlank(file.getOriginalFilename())) {
        		//文件上传
            	OSSClientUtil util = new OSSClientUtil();
            	String newsImgs = util.uploadImg2Oss(file);
            	news.setNewsImgs(newsImgs);
        	}
        	newsInfoService.addNews(getAdmin().getId(), news);
        } catch (Exception e) {
            logger.error("新增新闻异常", e);
            return "1001";
        }
        return "200";
	 }
	 
	 /**
	  * 修改
	  */
	 @ResponseBody
	 @RequestMapping(value="/updateProject", method = RequestMethod.POST)
	 public String updateProject(HttpServletRequest request,
			 @RequestParam(value = "newsType", required = false) Integer newsType,
			 @RequestParam(value = "newsTitle", required = false) String newsTitle,
			 @RequestParam(value = "newsSummary", required = false) String newsSummary,
			 @RequestParam(value = "newsReleaseDate", required = false) String newsReleaseDate,
			 @RequestParam(value = "newsContent", required = false) String newsContent,
			 @RequestParam(value = "newsKeyWord", required = false) String newsKeyWord,
			 @RequestParam(value = "id", required = false) Integer id){
        try {
        	MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request;
        	MultipartFile file = fileRequest.getFile("file");
        	BussNewsInfo news = new BussNewsInfo();
        	news.setNewsType(newsType);
        	news.setNewsTitle(newsTitle);
        	news.setNewsSummary(newsSummary);
        	news.setNewsContent(newsContent);
        	news.setNewsKeyWord(newsKeyWord);
        	news.setId(id);
        	if(StringUtils.isNotBlank(newsReleaseDate)){
        		news.setNewsReleaseDate(DateCompareUtil.getDateToString(newsReleaseDate));
        	}
        	if (file != null && StringUtils.isNotBlank(file.getOriginalFilename())) {
        		//文件上传
            	OSSClientUtil util = new OSSClientUtil();
            	String newsImgs = util.uploadImg2Oss(file);
            	news.setNewsImgs(newsImgs);
        	}
        	newsInfoService.updateNews(getAdmin().getId(), news);
        } catch (Exception e) {
            logger.error("修改服务项目异常", e);
            return "1001";
        }
        return "200";
	 }
	 
	 /**
	  * 删除
	  */
	 @ResponseBody
	 @RequestMapping(value="/delete")
	 public String delete(@RequestParam(value="id",required = false) Integer id){
        try {
        	newsInfoService.deleteNews(getAdmin().getId(),id);
        } catch (Exception e) {
            logger.error("删除区域异常", e);
            return "1001";
        }
        return "200";
	 }
	 /**
	  * 查看详情
	  */
	 @RequestMapping(value="/newsInfo" ,method = {RequestMethod.GET})
	 public ModelAndView newsInfo(@RequestParam(value="id",required = false) Integer id){
			 ModelAndView view = new ModelAndView("news/news-info");
			 BussNewsInfo news = newsInfoService.findNewsInfo(id);
			 view.addObject("info", news);
			 return view;
	 }
}
